package com.hzdl.apple.service.cartService;

import java.sql.Connection;
import java.util.List;

import com.hzdl.apple.bean.cart.Order;
import com.hzdl.apple.bean.cart.OrderItem;
import com.hzdl.apple.dao.OrderDao;
import com.hzdl.apple.dao.imp.OrderDaoImp;

/**
 * 订单Service
 *
 */
public class OrderService {

	private OrderDao orderDao = new OrderDaoImp();

	/**
	 * 添加订单
	 * 
	 * @param order
	 * @return
	 */
	public String addOrder(Order order, Connection conn) {
		if (orderDao.addOrder(order, conn)) {
			return "生成订单成功！";
		} else {
			return "生成失败";
		}
	}

	public int getOID(Connection conn) {
		return orderDao.getId(conn);
	}

	public boolean addOrderItem(OrderItem item, Connection conn,int UID) {
		return orderDao.addOrderItem(item, conn,UID);
	}

	// 获取订单集合并添加订单项到订单的list成员变量中
	public List<Order> getOrder(int UID) {
		//获取用户的订单
		List<Order> list = orderDao.getOrder(UID);
		List<OrderItem> listItem = null;
		//通过OID获取用户的所有订单项
		for (Order order : list) {
			order.setOrderList(orderDao.getOrderItem(UID, order.getOID()));
//			add(orderDao.getOrderItem(UID, order.getOID()));
		}
		
//		for (Order order : list) {
//			for (OrderItem orderItem : listItem) {
//				if (order.getOID() == orderItem.getOID()) {
//					order.setOrderList(listItem);
//				}
//			}
//		}
		return list;//带有订单项的订单集合
	}
	
	public boolean changType(int OID,int type){
		return orderDao.changeTypeByUOID(OID, type);
	}
}
